// package com.mapresso;

/**
 * ***************************************************************************************
 * FRectangle
 * 
 * @author Adrian Herzog
 * @version 1.0
 */

public class FRectangle implements Cloneable {
	float xmin;
	float ymin;
	float xmax;
	float ymax;

	FRectangle() {
		reset();
	}

	FRectangle(float x, float y) {
		this(x, y, x, y);
	}

	private FRectangle(FPoint p) {
		this(p.x, p.y, p.x, p.y);
	}

	FRectangle(FPoint p1, FPoint p2) {
		this(p1);
		checkPoint(p2);
	}

	/*
	 * private FRectangle (FRectangle p) { this(p.xmin, p.ymin);
	 * checkPoint(p.xmax, p.ymax); }
	 */
	FRectangle(double xmi, double ymi, double xma, double yma) {
		this((float) xmi, (float) ymi, (float) xma, (float) yma);
	}

	FRectangle(float xmi, float ymi, float xma, float yma) {
		xmin = xmi;
		ymin = ymi;
		xmax = xma;
		ymax = yma;
	}

	public Object clone() {
		FRectangle f;
		try {
			f = (FRectangle) super.clone();
		} catch (final CloneNotSupportedException ignore) {
			f = null;
		}
		return f;
	}

	void checkPoint(float x, float y) {
		if (x < xmin) {
			xmin = x;
		}

		if (y < ymin) {
			ymin = y;
		}

		if (x > xmax) {
			xmax = x;
		}

		if (y > ymax) {
			ymax = y;
		}

	}

	void checkPoint(FPoint f) {
		checkPoint(f.x, f.y);
	}

	void reset() {
		xmin = Float.MAX_VALUE;
		ymin = Float.MAX_VALUE;
		xmax = (float) -999999999.9; // gab Probleme: Float.MIN_VALUE;
		ymax = (float) -999999999.9;
// ymax = Float.MIN_VALUE;
	}

	void checkRectangle(FRectangle r) {
		if (r.xmin < xmin) {
			xmin = r.xmin;
		}

		if (r.ymin < ymin) {
			ymin = r.ymin;
		}

		if (r.xmax > xmax) {
			xmax = r.xmax;
		}

		if (r.ymax > ymax) {
			ymax = r.ymax;
		}

		checkMinMax();
	}

	/*
	 * public void zoomOut(double factor) { zoomOut(factor, new FPoint((float)
	 * (xmin + xmax) / 2.0f, (float) (ymin + ymax) / 2.0f)); }
	 */

	FRectangle pan(FPoint from, FPoint to) {
		float dx;
		dx = from.x - to.x;
		float dy;
		dy = from.y - to.y;
		xmin = xmin + dx;
		xmax = xmax + dx;
		ymin = ymin + dy;
		ymax = ymax + dy;
		return new FRectangle(xmin, ymin, xmax, ymax);
	}

	FRectangle zoomOut(double factor, FPoint p) {
		final float f = (float) factor;
		final float dx = f * (xmax - xmin) / 2.0f;
		final float dy = f * (ymax - ymin) / 2.0f;
		xmin = p.x - dx;
		xmax = p.x + dx;
		ymin = p.y - dy;
		ymax = p.y + dy;
		return new FRectangle(xmin, ymin, xmax, ymax);
	}

	public String toString() {
		return "FRectangle[xmin=" + xmin + ",ymin=" + ymin + ",xmax=" + xmax
				+ ",ymax=" + ymax + "]";
	}

	void notOutside(FRectangle rectmin) {
		checkMinMax();
		final float dx = xmax - xmin;
		final float dy = ymax - ymin;
		if (xmin < rectmin.xmin) {
			xmin = rectmin.xmin;
			xmax = xmin + dx;
		}
		if (xmax > rectmin.xmax) {
			xmax = rectmin.xmax;
			xmin = xmax - dx;
		}
		if (ymin < rectmin.ymin) {
			ymin = rectmin.ymin;
			ymax = ymin + dy;
		}
		if (ymax > rectmin.ymax) {
			ymax = rectmin.ymax;
			ymin = ymax - dy;
		}
		if (xmin < rectmin.xmin) {
			xmin = rectmin.xmin;
		}
		if (xmin < rectmin.xmin) {
			xmin = rectmin.xmin;
		}
		if (ymin < rectmin.ymin) {
			ymin = rectmin.ymin;
		}
		if (xmax > rectmin.xmax) {
			xmax = rectmin.xmax;
		}
		if (ymax > rectmin.ymax) {
			ymax = rectmin.ymax;
		}
	}

	private void checkMinMax() {
		float tmp;
		if (xmin > xmax) {
			tmp = xmin;
			xmin = xmax;
			xmax = tmp;
		}
		if (ymin > ymax) {
			tmp = ymin;
			ymin = ymax;
			ymax = tmp;
		}
	}

}
